/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doanjava3;

import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.JTable;

/**
 *
 * @author BaoAnh
 */
class clsQuery implements Serializable
{

    private ArrayList<JTable> lstTable = new ArrayList<JTable>();
    private String strSQL = "";

    public clsQuery(String strsql)
    {
        strSQL  = strsql;
    }

    clsQuery(String strsql, ArrayList<JTable> lsttbope)
    {
        strSQL = strsql;
        strSQL = formateSQL();
        lstTable = lsttbope;
    }

    JTable queryDadabase()
    {
        //chuan hoa cau truy van thanh chu in thuong
        strSQL = strSQL.toLowerCase();
        //kiem tra neu cau truy van co chua tu select thi goi ham selectSQL
        if (strSQL.contains("select"))
        {
            return selectSQL();
        }
        //kiem tra neu cau truy van co chua tu insert thi goi ham insertSQL    
        else if (strSQL.contains("insert"))
        {
            return insertSQL();

        }
        //kiem tra neu cau truy van co chua tu delete thi goi ham deleteSQL
        else if (strSQL.contains("delete"))
        {
            return deleteSQL();
        }
        //neu nhu cau truy van khong co nhung tu tren thi tra ve bang NULL
        else
            return null;
    }

    /**
     * @return the lstTBOpe
     */
    public ArrayList<JTable> getLstTBOpe()
    {
        return lstTable;
    }

    /**
     * @param lstTBOpe the lstTBOpe to set
     */
    public void setLstTBOpe(ArrayList<JTable> lstTBOpe)
    {
        this.lstTable = lstTBOpe;
    }

    /**
     * @return the strSQL
     */
    public String getStrSQL()
    {
        return strSQL;
    }

    /**
     * @param strSQL the strSQL to set
     */
    public void setStrSQL(String strSQL)
    {
        this.strSQL = strSQL;
    }

    private JTable selectSQL()
    {
        //neu select co dieu kien
        if (strSQL.contains("where"))
        {
            return selectSQLCondition();
        }
        //neu select khong co dieu kien
        else
        {
            return selectSQLNoncondition();
        }
    }

    private JTable insertSQL()
    {
        return null;
    }

    private JTable deleteSQL()
    {
        return null;
    }

    private JTable selectSQLCondition()
    {
        return null;
    }

    private JTable selectSQLNoncondition()
    {
        //neu trong cau truy van co dau * (truy van tat ca)
        if (strSQL.contains("*"))
        {
            return selectSQLNonConditionStar();
        }
        //neu trong cau truy van khong co dau * (truy van vai thuoc tinh cua bang)
        else
        {
            return selectSQLNonConditionNonstar();
        }
    }

    private JTable selectSQLNonConditionStar()
    {
        //day la truong hop truy van tat ca cac thuoc tinh cua bang ma khong co dieu kien
        String strtemp = "select * from ";
        //lay ten bang trong cau truy van
        String strName1 = strSQL.substring(strtemp.length()) + "open";
        String strName2 = "";
        System.out.println(strName1);
        for(int i = 0; i < lstTable.size(); i ++){
            strName2 = lstTable.get(i).getName().toLowerCase();
            if(strName1.equals(strName2)){
                return lstTable.get(i);
            }
        }
        return null;
    }

    private JTable selectSQLNonConditionNonstar()
    {
        return null;
    }

    public String formateSQL()
    {
        //xoa cac khoang trang du thua giua cac chu, chi chua lai 1 khoang trang.
        strSQL = strSQL.replaceAll("\\s+", " ");
        if(strSQL.substring(0, 1) == " "){
            strSQL = strSQL.substring(1);   
        }
        return strSQL;
    }
}
